import { createRouter, createWebHashHistory } from 'vue-router'
// import Login from '../components/login.vue'
const Login = () => import(/* webpackChunkName: "home_login_welcome" */ '../components/login.vue')
// import Home from '../components/home.vue'
const Home = () => import(/* webpackChunkName: "home_login_welcome" */ '../components/home.vue')
// import Welcome from '../components/welcome.vue'
const Welcome = () => import(/* webpackChunkName: "home_login_welcome" */ '../components/welcome.vue')
// import Users from '../components/Users/users.vue'
const Users = () => import(/* webpackChunkName: "users" */ '../components/Users/users.vue')
// import Roles from '../components/Power/roles.vue'
const Roles = () => import(/* webpackChunkName: "power" */ '../components/Power/roles.vue')
// import Rights from '../components/Power/rights.vue'
const Rights = () => import(/* webpackChunkName: "power" */ '../components/Power/rights.vue')
// import Params from '../components/goods/params.vue'
const Params = () => import(/* webpackChunkName: "goods" */ '../components/goods/params.vue')
// import Orders from '../components/order/orders.vue'
const Orders = () => import(/* webpackChunkName: "order" */ '../components/order/orders.vue')
// import Goods from '../components/goods/goods.vue'
const Goods = () => import(/* webpackChunkName: "goods" */ '../components/goods/goods.vue')
// import Categories from '../components/goods/categories.vue'
const Categories = () => import(/* webpackChunkName: "goods" */ '../components/goods/categories.vue')
// import Reports from '../components/Users/reports.vue'
const Reports = () => import(/* webpackChunkName: "users" */ '../components/Users/reports.vue')
// import Add from '../components/goods/add.vue'
const Add = () => import(/* webpackChunkName: "goods" */ '../components/goods/add.vue')
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/roles', component: Roles },
      { path: '/rights', component: Rights },
      { path: '/params', component: Params },
      { path: '/orders', component: Orders },
      { path: '/goods', component: Goods },
      { path: '/reports', component: Reports },
      { path: '/categories', component: Categories },
      { path: '/goods/add', component: Add }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
// to 将要访问的路径
// from 从哪个路径跳转而来
// next 函数，代表放行
// next() 放行  next(./login) 强制跳转
  if (to.path === '/login') return next()
  //  获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
